<template>
  <div class="app-container">
    <el-dialog title="选择保养项" :visible.sync="open" width="800px" v-dialogDrag append-to-body @close='close'>
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="保养类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择保养类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_MAINTENANCE_PLAN_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="chooseEntry">选择</el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection"/>
      <el-table-column label="项目编号" align="center" prop="id" />
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="保养类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_MAINTENANCE_PLAN_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="工时(时)" align="center" prop="workHours" />
      <el-table-column label="保养内容" align="center" prop="content" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="保养手册" align="center" prop="manual" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    </el-dialog>
  </div>
</template>

<script>
import {getMaintenancePlanEntryNotSelectPage} from "@/api/device/maintenancePlanEntry";
import FileUpload from '@/components/FileUpload';
import {createBatchMaintenanceDeviceEntry} from "@/api/device/maintenanceDeviceEntry";

export default {
  name: "MaintenancePlanEntry",
  components: {
    FileUpload,
  },
  props:{
    openMaintenanceSelectEntry: {
      required: true,
      default: false
    },
    deviceId: {
      required: true,
      default: ""
    }
  },
  watch: {
    openMaintenanceSelectEntry(newVal){
      this.open = newVal;
      this.getList()
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 保养项选择数据
      selectEntryList:[],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备保养项目列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: this.openMaintenanceSelectEntry,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        status: 0,
        type: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 选择保养项*/
    chooseEntry(){
      if (this.selectEntryList.length <= 0) {
        this.$modal.msgWarning("请至少选择一个检查项!")
        return;
      }
      let deviceEntry = []
      this.selectEntryList.forEach(item => {
        deviceEntry.push({"deviceId": this.deviceId, "entryId": item.id})
      })
      createBatchMaintenanceDeviceEntry(deviceEntry).then(response => {
        this.$modal.msgSuccess("选择成功");
        this.close()
      });
    },
    /** 保养项多选数据*/
    handleSelectionChange(selection){
      this.selectEntryList = selection
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.deviceId=  this.deviceId
      // 执行查询
      getMaintenancePlanEntryNotSelectPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        status: undefined,
        type: undefined,
        workHours: undefined,
        content: undefined,
        remark: undefined,
        manual: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  close() {
    this.open = false;
    this.$emit('update:openMaintenanceSelectEntry', this.open)
    this.$emit('getMaintenanceEntryList')
  }
  }
};
</script>
